** Comparative Study of Electoral Systems
cd "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/CSES"

*****
use "Raw/cses1.dta", clear
keep if A1004=="AUS_1996" | A1004=="CHE_1999" | A1004=="ESP_2000" | A1004=="NOR_1997" | A1004=="NZL_1996" 
rename (A1004 A1015 A3032* A3031 A2003) (E1004 E1015 E3019* E3020 E2003)
keep E* A2028 A2030 A2031
replace A2030=A2031 if A2030==0
recode A2028 (5/9=0)
rename (A2028 A2030) (vote lastvote)
recode lastvote (1=2) (2=1) (3=3) (4=4) (5=5) (6=6) (else=.a) if E1004=="AUS_1996"
recode lastvote (1=3) (2=4) (3=2) (4=1) (5=6) (13=5) (else=.a) if E1004=="CHE_1999"
recode lastvote (1=1) (2=2) (4=3) (5=4) (6=5) (else=.a) if E1004=="ESP_2000"
recode lastvote (2=6) (3=1) (5=4) (6=5) (8=3) (9=2) (else=.a) if E1004=="NOR_1997"
recode lastvote (1=2) (2=1) (3=3) (4=4) (5=5) (6=6) (else=.a) if E1004=="NZL_1996"
recode E2003 (9=.a)
gen module=1
saveold "cses1 v2.dta", replace

use "Raw/cses2.dta", clear
keep if B1004=="AUS_2004" | B1004=="CHE_2003" | B1004=="ITA_2006" | B1004=="NLD_2002" | B1004=="NOR_2001" | B1004=="NZL_2002"
rename (B1004 B1015 B3038* B3045 B2003) (E1004 E1015 E3019* E3020 E2003)
keep E* B3004_1 B3006_1
recode B3004_1 (2/9=0)
recode B3006_1 (94/99=.a)
rename (B3004_1 B3006_1) (vote lastvote)
recode lastvote (1=1) (2=4) (3=2) (4=3) (5=5) (6=6) (7=7) (else=.a) if E1004=="AUS_2004"
recode lastvote (1=3) (2=4) (3=2) (4=1) (13=5) (else=.a) if E1004=="CHE_2003"
recode lastvote (7/26=.a) (missing=.a) if E1004=="ITA_2006"
recode lastvote (1=4) (2=1) (3=3) (4=7) (5=5) (6=9) (7=8) (9=2) (10=6) (else=.a) if E1004=="NLD_2002"
recode lastvote (2=4) (3=1) (4=7) (5=5) (6=6) (8=2) (9=3) (else=.a) if E1004=="NOR_2001"
recode lastvote (9/21=.a) (missing=.a) if E1004=="NZL_2002"
gen module=2
saveold "cses2 v2.dta", replace

use "Raw/cses3.dta", clear
keep if C1004=="AUS_2007" | C1004=="CHE_2007" | C1004=="DEU_2009" | C1004=="GRC_2009" | C1004=="NLD_2010" | C1004=="NOR_2005" | C1004=="NOR_2009" | C1004=="NZL_2008" | C1004=="PRT_2009" 
rename (C1004 C1015 C3011* C3013 C2003) (E1004 E1015 E3019* E3020 E2003)
keep E* C3021_1 C3023_LH_PL C3023_LH_DC
replace C3023_LH_PL=C3023_LH_DC if C3023_LH_PL==99
recode C3021_1 (5/9=0)
recode C3023_LH_PL (89/99=.a)
rename (C3021_1 C3023_LH_PL) (vote lastvote)
recode lastvote (1=2) (2=1) (missing=.a) if E1004=="AUS_2007"
recode lastvote (1=3) (2=4) (3=2) (4=1) (10=5) (6=6) (5=7) (else=.a) if E1004=="CHE_2007"
recode lastvote (2=1) (3=1) (4=3) (6=4) (5=5) (2=6) (8=7) (else=.a) if E1004=="DEU_2009"
recode lastvote (88=.a) (missing=.a) if E1004=="GRC_2009"
recode lastvote (10/15=.a) (missing=.a) if E1004=="NLD_2010"
recode lastvote (3=1) (8=2) (7=3) (2=4) (5=5) (6=6) (4=7) (1=8) (else=.a) if E1004=="NOR_2005"
recode lastvote (3=1) (8=2) (7=3) (2=4) (6=5) (5=6) (4=7) (1=8) (else=.a) if E1004=="NOR_2009"
recode lastvote (9/20=.a) (missing=.a) if E1004=="NZL_2008"
recode lastvote (5=1) (4=2) (2=3) (1=4) (3=5) (else=.a) if E1004=="PRT_2009"
gen module=3
saveold "cses3 v2.dta", replace

use "Raw/cses4_3.dta", clear
keep if D1004=="AUS_2013" | D1004=="CHE_2011" | D1004=="SWE_2014" | D1004=="NOR_2013" | D1004=="NZL_2011" | D1004=="PRT_2015" | D1004=="FIN_2015" | D1004=="GBR_2015" | D1004=="GRC_2015" | D1004=="NZL_2014"
rename (D1004 D1015 D3013* D3014 D2003) (E1004 E1015 E3019* E3020 E2003) 
keep E* D3006_LH_PL D3006_LH_DC
replace D3006_LH_PL=D3006_LH_DC if D3006_LH_PL==99
recode D3006_LH_PL (1/90=1) (else=0), gen(vote)
rename D3006_LH_PL lastvote
recode lastvote (6/18=.a) (89/99=.a) if E1004=="AUS_2013"
recode lastvote (4=1) (3=2) (1=3) (2=4) (11=5) (10=6) (9=7) (else=.a) if E1004=="CHE_2011"
recode lastvote (2=1) (5=2) (8=3) (7=4) (3=5) (1=6) (4=7) (6=8) (7=9) (else=.a) if E1004=="SWE_2014"
recode lastvote (90/98=.a) if E1004=="NOR_2013"
recode lastvote (2=1) (1=2) (10/13=.a) (90/99=.a) if E1004=="NZL_2011"
recode lastvote (90/99=.a) if E1004=="PRT_2015"
recode lastvote (9/99=.a) if E1004=="FIN_2015"
recode lastvote (90/99=.a) if E1004=="GBR_2015"
recode lastvote (90/99=.a) if E1004=="GRC_2015"
recode lastvote (90/99=.a) if E1004=="NZL_2014"
gen module=4
saveold "cses4 v2.dta", replace

use "Raw/cses5.dta", clear
keep if inlist(E1004, "AUS_2019", "AUT_2017", "DEU_2017", "FRA_2017", "GRC_2015", "IRL_2016", "ITA_2018", "NZL_2017", "NOR_2017") 
foreach party in A B D{
replace E3019_`party'=E3021_`party' if E1004=="FRA_2017" /* See Election Study Notes */
}
keep E1004 E1015 E3019* E3020 E3013_LH_PL E3013_LH_DC E3013_PR_1 E2003
replace E3013_LH_PL=E3013_LH_DC if E3013_LH_PL==999995
replace E3013_LH_PL=E3013_PR_1 if E3013_LH_PL==999996
recode E3013_LH_PL (1/999993=1) (999997/999999=0), gen(vote)
rename E3013_LH_PL lastvote
replace lastvote=lastvote-36000 if E1004=="AUS_2019" & lastvote<=36037
recode lastvote (38/999999=.a) if E1004=="AUS_2019"
replace lastvote=lastvote-40000 if E1004=="AUT_2017" & lastvote<=40007
recode lastvote (8/999999=.a) if E1004=="AUT_2017"
replace lastvote=lastvote-276000 if E1004=="DEU_2017" & lastvote<=2760009
recode lastvote (9=1) (10/999999=.a) if E1004=="DEU_2017"
replace lastvote=lastvote-250000 if E1004=="FRA_2017" & lastvote<=250011
recode lastvote (12/999999=.a) if E1004=="FRA_2017"
replace lastvote=lastvote-300000 if E1004=="GRC_2015" & lastvote<=300008
recode lastvote (9/999999=.a) if E1004=="GRC_2015"
replace lastvote=lastvote-372000 if E1004=="IRL_2016" & lastvote<=372008
recode lastvote (9/999999=.a) if E1004=="IRL_2016"
replace lastvote=lastvote-380000 if E1004=="ITA_2018" & lastvote<=380006
recode lastvote (7/999999=.a) if E1004=="ITA_2018"
replace lastvote=lastvote-578000 if E1004=="NOR_2017" & lastvote<=578009
recode lastvote (10/999999=.a) if E1004=="NOR_2017"
replace lastvote=lastvote-554000 if E1004=="NZL_2017" & lastvote<=554013
recode lastvote (14/999999=.a) if E1004=="NZL_2017"

gen module=5
saveold "cses5 v2.dta", replace

*****
use "cses5 v2.dta", clear
append using "cses4 v2.dta"
append using "cses3 v2.dta"
append using "cses2 v2.dta"
append using "cses1 v2.dta"

recode E2003 (96=0) (97/99=.a)
recode E3020 E3019* (11/99=.a)
rename E3020 resleft
rename E3019_*? left#, renumber

gen iso=.a
replace iso=1036 if E1004=="AUS_1996" | E1004=="AUS_2004" | E1004=="AUS_2007" | E1004=="AUS_2013" | E1004=="AUS_2019"
replace iso=1040 if E1004=="AUT_2017"
replace iso=1246 if E1004=="FIN_2015"
replace iso=1250 if E1004=="FRA_2017"
replace iso=1276 if E1004=="DEU_2009" | E1004=="DEU_2017" 
replace iso=1300 if E1004=="GRC_2009" | E1004=="GRC_2015" 
replace iso=1372 if E1004=="IRL_2016"
replace iso=1380 if E1004=="ITA_2006" | E1004=="ITA_2018"
replace iso=1528 if E1004=="NLD_2002" | E1004=="NLD_2010"
replace iso=1554 if E1004=="NZL_1996" | E1004=="NZL_2002" | E1004=="NZL_2008" | E1004=="NZL_2011" | E1004=="NZL_2014" | E1004=="NZL_2017"
replace iso=1578 if E1004=="NOR_1997" | E1004=="NOR_2001" | E1004=="NOR_2005" | E1004=="NOR_2009" | E1004=="NOR_2013" | E1004=="NOR_2017"
replace iso=1620 if E1004=="PRT_2009" | E1004=="PRT_2015"
replace iso=1724 if E1004=="ESP_2000"
replace iso=1752 if E1004=="SWE_2014"
replace iso=1756 if E1004=="CHE_1999" | E1004=="CHE_2003" | E1004=="CHE_2007" | E1004=="CHE_2011"
replace iso=1826 if E1004=="GBR_2015"

sort iso module E1004
egen panel=group(iso module E1004)

/* Non-legislative, minor parties w/ no vote share in the CMP data are coded as missing for the political knowledge scale */
replace left5=.a if E1004=="AUS_1996"	/* Module 1 */
replace left6=.a if E1004=="NZL_1996"	

replace left6=.a if E1004=="AUS_2004" 	/* Module 2 */
replace left7=.a if E1004=="AUS_2004" 
replace left4=.a if E1004=="ITA_2006"
replace left6=.a if E1004=="ITA_2006"
replace left8=.a if E1004=="NZL_2002"

replace left6=.a if E1004=="DEU_2009"	/* Module 3 */
replace left6=.a if E1004=="GRC_2009"
replace left4=.a if E1004=="NZL_2008"
replace left8=.a if E1004=="NOR_2005"
replace left8=.a if E1004=="NOR_2009"

replace left9=.a if E1004=="NOR_2013"	/* Module 4 */
replace left5=.a if E1004=="NZL_2014"	
replace left6=.a if E1004=="NZL_2014"
replace left6=.a if E1004=="PRT_2015"
replace left8=.a if E1004=="PRT_2015"
replace left9=.a if E1004=="SWE_2014"	

replace left5=.a if E1004=="AUS_2019"	/* Module 5 */
replace left6=.a if E1004=="AUS_2019"	
replace left7=.a if E1004=="DEU_2017"
replace left8=.a if E1004=="DEU_2017"
replace left6=.a if E1004=="FRA_2017"
replace left8=.a if E1004=="IRL_2016"
replace left5=.a if E1004=="NZL_2017" 	
replace left8=.a if E1004=="NZL_2017" 	/* 26 parties in total, unmatched when merged  */

forval i=1/9 {
by panel, sort :egen mleft`i'=mean(left`i')
gen abserr`i'=abs(left`i'-mleft`i') if !missing(left`i')
}

levelsof panel, loc(levels)
foreach c of loc levels {
capture forval i=1/9{
sum abserr`i' if panel==`c'
replace abserr`i'=`r(mean)'+1.96*`r(sd)' if missing(abserr`i') & mleft`i'!=.a & panel==`c' 
}
}

gen knowsca=.a
gen knowsca_alt=.a
gen educsca=.a
egen polknow=rowtotal(abserr*), missing
levelsof panel, loc(levels)
foreach c of loc levels {
egen knowsca`c'=cut(polknow) if panel==`c', group(3)
replace knowsca=knowsca`c' if panel==`c'
egen knowsca_alt`c'=cut(polknow) if panel==`c', group(4)
replace knowsca_alt=knowsca_alt`c' if panel==`c'
_pctile E2003 if panel==`c', p(60, 99.99)
replace educsca=0 if E2003<=`r(r1)' & panel==`c' & !missing(E2003)
replace educsca=1 if E2003>`r(r1)' & E2003<=`r(r2)' & panel==`c' & !missing(E2003)
drop knowsca`c' knowsca_alt`c'
}
tabstat educsca, stats(min max mean) by(E1004)
replace educsca=1 if E2003==8 & E1004=="NOR_2009"							/* Manual recoding for Norway (2009) - cutpoint is .5949 */

gen ideoknow=0
replace ideoknow=1 if mleft1<mleft2 & left1<left2 & left1!=.a & left2!=.a
replace ideoknow=1 if mleft1>mleft2 & left1>left2 & left1!=.a & left2!=.a

by panel, sort: egen rescenter=mean(resleft)

by panel, sort: egen resleftsop_alt4=sd(resleft) if knowsca_alt==3
by panel, sort: egen resleftsop_alt3=sd(resleft) if knowsca_alt==2
by panel, sort: egen resleftsop_alt2=sd(resleft) if knowsca_alt==1
by panel, sort: egen resleftsop_alt1=sd(resleft) if knowsca_alt==0
replace resleftsop_alt3=resleftsop_alt4 if E1004=="ITA_2006"				/* Manual recoding for Italy (2006) - cutpoint is .4753 */

by panel, sort: egen medianv=median(resleft)
by panel, sort: egen resleftvot=sd(resleft) if vote==1
by panel, sort: egen resleftabs=sd(resleft) if vote==0
by panel, sort: egen resleftsop3=sd(resleft) if knowsca==2
by panel, sort: egen resleftsop2=sd(resleft) if knowsca==1
by panel, sort: egen resleftsop1=sd(resleft) if knowsca==0
by panel, sort: egen resleftpkn=sd(resleft) if ideoknow==1
by panel, sort: egen resleftpuk=sd(resleft) if ideoknow==0
by panel, sort: egen reslefteducl=sd(resleft) if educsca==0
by panel, sort: egen reslefteduch=sd(resleft) if educsca==1

replace E1004="GRC_2016" if E1004=="GRC_2015" & module==5					/* Greece had two elections in 2015, 2015-09 is renamed as 2016 */ 
collapse (sd) resleft (mean) rescenter-reslefteduch left* iso module vote, by(E1004)
reshape long left, i(E1004) j(party)
drop if missing(left)
order iso E1004 party, first
saveold "cses_merged v1.dta", replace

*****/
import excel "/Users/mmoral/Dropbox/Sabancı University/-ONGOING/On the Relationship between Party Polarization and Citizen Polarization/Parties CSES v9.xlsx", sheet("Stata Corrected") firstrow clear
saveold "Stata Corrected_CSES.dta", replace

use "cses_merged v1.dta", clear
rename party pid
merge 1:1 E1004 pid iso using "Stata Corrected_CSES.dta"
rename vote turnout_sample
generate str study = "CSES" + string(module)
drop _merge module E1004
order study countryname iso date pid party partyname resleft resleftvot resleftabs resleftsop3 resleftsop2 resleftsop1 resleftpkn resleftpuk resleftsop_alt* rescenter medianv left, first
saveold "cses_merged v2.dta", replace
